package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class showmessage
 */
@WebServlet("/src/showmessage")
public class showmessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showmessage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String receive_identity = request.getParameter("receive_identity");
	    String receive_id = request.getParameter("receive_id");
	    
	    hello a = new hello();//模板
	    try {
			ResultSet b;
				b=a.doselect("SELECT "
					    +" CASE "
					    +" WHEN message.message_sender = 'student' THEN student.student_name "
				        +" WHEN message.message_sender = 'teacher' THEN teacher.teacher_name "
				        +" WHEN message.message_sender = 'secretary' THEN secretary.secretary_name "
				        +" ELSE 'Unknown' "
				    +" END AS sender_name,"
				    +" message.message_content "
				+" FROM message "
				+" LEFT JOIN student ON message.message_sender = 'student' AND message.message_senderid = student.student_id "
				+" LEFT JOIN teacher ON message.message_sender = 'teacher' AND message.message_senderid = teacher.teacher_id "
				+" LEFT JOIN secretary ON message.message_sender = 'secretary' AND message.message_senderid = secretary.secretary_id "
				+" WHERE message.message_object = ? AND message.message_objectid = ? ",receive_identity,receive_id);		
				
			while(b.next()) {//b代表查询结果，查询之后的那个表，next（）函数就是往下一行，逐行扫描
				response.getWriter().write(b.getString("sender_name")+";");//这里表示的是往返回的那个response里面添加字符串，修改write括号后的内容即可
				response.getWriter().write(b.getString("message_content")+";");
			}//模板
			b.close();
	    
	    }catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板
	}

}
